//
//  UIViewController+addNoNetView.m
//  ShunLianPower2
//
//  Created by 陈镇 on 2017/12/2.
//  Copyright © 2017年 陈镇. All rights reserved.
//

#import "UIViewController+addNoNetView.h"
#import <objc/runtime.h>
#import "SLNetworkMonitorManager.h"
#import <QMUIKit/UIViewController+QMUI.h>
#import <QMUIKit/UIColor+QMUI.h>
#import <QMUIKit/UIView+QMUI.h>

static NSString *noNetKey = @"noNetKey";
@implementation UIViewController (addNoNetView)

+ (void)load {
    ExchangeImplementations([UIViewController class], @selector(viewDidAppear:), @selector(sl_viewDidAppear:));
}

- (void)sl_viewDidAppear:(BOOL)animated {
    [self sl_viewDidAppear:animated];
    [self showNoNetView];
}

- (void)showNoNetView {
    if (![SLNetworkMonitorManager sheardManager].isHaveNet) {
        [self createNetView];
    }else{
        [self dismissNoNetView];
    }
}

- (void)setIsnoNetView:(BOOL)isnoNetView{
    objc_setAssociatedObject(self, &noNetKey, @(isnoNetView), OBJC_ASSOCIATION_COPY);
}

- (BOOL)isnoNetView{
    return [objc_getAssociatedObject(self, &noNetKey) integerValue];
}

- (void)createNetView{
    BOOL isContains = [[QMUIHelper visibleViewController].navigationController.childViewControllers containsObject:self];
    if (!self.isnoNetView && isContains) {
        CGFloat noNetViewOriginY = 0;
        /*
        iOS7在Conttoller中新增了这个属性：automaticallyAdjustsScrollViewInsets= yes,
        或者:edgesForExtendedLayout = UIRectEdgeNone;
        如果视图里面存在唯一一个UIScrollView或其子类View，那么它会自动设置相应的内边距，
        这样可以让scroll占据整个视图，又不会让导航栏遮盖
        self.edgesForExtendedLayout = UIRectEdgeNone;//scrollview出现下移
         
        不管navigationBar的backgroundImage如何设置，都让布局撑到屏幕顶部，方便布局的统一
        self.extendedLayoutIncludesOpaqueBars = YES;
        */
        //半透明的默认YES
//        if (self.extendedLayoutIncludesOpaqueBars == YES) {
//            if (self.edgesForExtendedLayout == UIRectEdgeNone) {
//                if ([[self valueForKey:@"navigationHidden"] boolValue] == YES) {
//                    noNetViewOriginY = NavigationContentTop;
//                }else {
//                    noNetViewOriginY = 0;
//                }
//            }else {
//                noNetViewOriginY = NavigationContentTop;
//            }
//        }
//        UIView *noNetView = [[UIView alloc] initWithFrame:CGRectMake(0, noNetViewOriginY, SCREEN_WIDTH, 40)];
//        noNetView.tag = 12307;
//        noNetView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.6];

//        NSBundle *mainBundle = [NSBundle bundleForClass:[SLNoNetViewController class]];
//        NSString *resourcePath = [mainBundle pathForResource:@"SLBusinessBaseResources" ofType:@"bundle"];
//        NSBundle *resourceBundle = [NSBundle bundleWithPath:resourcePath] ?: mainBundle;
//        
//        UIImageView *leftView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 13, 13, 13)];
//        leftView.image = [UIImage imageNamed:@"sl_no_network_tips" inBundle:resourceBundle compatibleWithTraitCollection:nil];
//        [noNetView addSubview:leftView];
//
//        UIImageView *rightView= [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 9 - 15, 12, 9, 16)];
//        rightView.image = [UIImage imageNamed:@"sl_bussnessbase_arrow_right_white" inBundle:resourceBundle compatibleWithTraitCollection:nil];
//        [noNetView addSubview:rightView];

//        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(leftView.qmui_right +5, leftView.qmui_top, rightView.qmui_left - leftView.qmui_right - 10, leftView.qmui_height)];
//        title.text = @"网络连接异常，请检查您的网络设置";
//        title.font = UIFontMake(14);
//        title.textColor = UIColorWhite;
//        [noNetView addSubview:title];
//        self.isnoNetView = YES;
//        [self.view addSubview:noNetView];
        
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickNoNetView)];
//        [noNetView addGestureRecognizer:tap];
    }else{
        UIView *noNetView = [self.view viewWithTag:12307];
        if (noNetView) {
            [self.view bringSubviewToFront:noNetView];
        }
    }
    
}

- (void)dismissNoNetView{
    self.isnoNetView = NO;
    UIView *view = [self.view viewWithTag:12307];
    [view removeFromSuperview];
}

- (void)clickNoNetView{
//    SLNoNetViewController *vc  =[[SLNoNetViewController alloc] init];
//    [self.navigationController pushViewController:vc animated:YES];
}
@end
